from aria_tests import *

def html_menubar_element_start_code(f, test):
  # Check for aria-labelledy references
  alabel_list = test['aria-labelledby']
  if len(alabel_list):
    i = 1
    for item in alabel_list:
      f.write("   <div id='alabel_" + str(i) + "'>" + item + "</div>\n")      
      i += 1
    
  f.write("      <ul id='test1' role='menubar'\n")
  
  # Check for aria-describedby attribute
  if test['aria-describedby']:
    f.write("          aria-describedby='test_desc'\n")      
     
  # Add related ARIA Properties
  for property in test['properties']:
    [p, value] = property.split('=')
    f.write("          " + p + "='" + value + "'\n")
    
  # Add related ARIA States
  for state in test['states']:
    [s, value] = state.split('=')
    f.write("          " + s + "='" + value + "'\n")
    
  # Check for title attribute
  if test['title']:
    f.write("          title='" + test['title'] + "'\n")      

  # Check for aria-label attribute
  if test['aria-label']:
    f.write("          aria-label='" + test['aria-label'] + "'\n")      

  # Check for aria-labelledy references
  if len(alabel_list):
    f.write("          aria-labelledby='")
    i = 1    
    for item in alabel_list:
      f.write("alabel_" + str(i) + " " )      
      i += 1
    f.write("'\n")        
 
  f.write("      >\n")

def html_menubar_element_end_code(f, test):

  # End menubar container
  f.write("    </ul>\n")

 # if any aria-describedby content, add it to the page
  if test['aria-describedby']:
    f.write("    <p id='test_desc'>" + test['aria-describedby'] + "</p>\n")

def add_attributes( f, attributes):
  str = ""
  # Add related ARIA States
  for attr in attributes:
    [a, value] = attr.split('=')
    str += " " + a + "='" + value + "'"
  return str  

def html_menubar_popup_test_code(f, popup, spacing ):

  f.write(spacing + "<ul role='menu'>\n")
  for item in popup: 
    if len(item['popup']):
      f.write(spacing + "  <li id='" + item['id'] + "' role='" + item['role'] + "' tabindex='0' " + add_attributes( f, item['attributes']) + ">" + item['content'] + "\n")
      html_menubar_popup_test_code(f, popup, (spacing + "    ") )
      f.write(spacing + "  </li>\n")     
    else:
      f.write("        <li id='" + item['id'] + "' role='" + item['role'] + "' tabindex='0' " + add_attributes( f, item['attributes']) + ">" + item['content'] + "</li>\n")
  f.write(spacing + "</ul>\n")

def html_menubar_items_test_code(f, test ):

  for item in test['menu']: 
    if len(item['popup']):
      f.write("        <li id='" + item['id'] + "' role='" + item['role'] + "' tabindex='0' " + add_attributes( f, item['attributes']) + ">" + item['content'] + "\n")
      html_menubar_popup_test_code(f, item['popup'], "          " )
      f.write("        </li>\n")
    else:
      f.write("        <li id='" + item['id'] + "' role='" + item['role'] + "'  tabindex='0'" + add_attributes( f, item['attributes']) + ">" + item['content'] + "</li>\n")


def html_menubar_child_node_test_code(f, test ):

  html_menubar_element_start_code(f, test)
  html_menubar_items_test_code(f, test )
  html_menubar_element_end_code(f, test)

def make_menubar_test( group, id, title, path, fname, test):
  print "\nCreating  " + path + fname + "...\n"
  
  fname_test    = "test_" + fname + ".html";
  fname_results = "results_" + fname + ".js";
  
  f = open(path+fname_test,    'w')
  
  if not os.path.isfile(path+fname_results):
    s = open(path+fname_results, 'w')
    make_results_file( s, id, path, fname_test, title, test['description'], 'role', 'slider', test, group )

  path = '../'
  html_head_start(f, path, title)
  html_head_style_link(f, path, 'aria_menubar.css', 'all')
  script_add_results_ref( f, fname_results )
  script_reference( f, path, 'aria_menubar.js')
  script_set_focus( f, test['test_id'] )
  html_body_start(f, id, title, test)
  html_menubar_child_node_test_code(f, test)
  html_accessible_name(f, test)
  html_api_mappings(f, test)
  html_body_end(f)
  return fname_results  


menu_item = [
  { 'role'       : 'menuitem',
    'id'         : 'mi1',
    'content'    : 'Menu Item 1',
    'attributes' : [],
    'popup'      : [],
  },
  { 'role'       : 'menuitem',
    'id'         : 'mi2',
    'content'    : 'Menu Item 2',
    'attributes' : [],
    'popup'      : [],
  },
  { 'role'       : 'menuitem',
    'id'         : 'mi3',
    'content'    : 'Menu Item 3',
    'attributes' : [],
    'popup'      : [],
  },
  { 'role'       : 'menuitem',
    'id'         : 'mi4',
    'content'    : 'Menu Item 4',
    'attributes' : [],
    'popup'      : [],
  },
]

menu_checkbox = [
  { 'role'       : 'menuitemcheckbox',
    'id'         : 'micb1',
    'content'    : 'Checkbox Item 1',
    'attributes' : [],
    'popup'      : [],
  },
  { 'role'       : 'menuitemcheckbox',
    'id'         : 'micb2',
    'content'    : 'Checkbox Item 2',
    'attributes' : ["aria-checked=false"],
    'popup'      : [],
  },
  { 'role'       : 'menuitemcheckbox',
    'id'         : 'micb3',
    'content'    : 'Checkbox Item 3',
    'attributes' : ["aria-checked=true"],
    'popup'      : [],
  },
  { 'role'       : 'menuitemcheckbox',
    'id'         : 'micb4',
    'content'    : 'Checkbox Item 4',
    'attributes' : ["aria-checked=mixed"],
    'popup'      : [],
  },
]

menu_radio = [
  { 'role'       : 'menuitemradio',
    'id'         : 'mirb1',
    'content'    : 'Radio Item 1',
    'attributes' : [],
    'popup'      : [],
  },
  { 'role'       : 'menuitemradio',
    'id'         : 'mirb2',
    'content'    : 'Radio Item 2',
    'attributes' : ["aria-checked=true"],
    'popup'      : [],
  },
  { 'role'       : 'menuitemradio',
    'id'         : 'mirb3',
    'content'    : 'Radio Item 3',
    'attributes' : ["aria-checked=false"],
    'popup'      : [],
  },
  { 'role'       : 'menuitemradio',
    'id'         : 'mirb4',
    'content'    : 'Radio Item 4',
    'attributes' : [],
    'popup'      : [],
  },
]


menu_popup = [
  { 'role'       : 'menuitem',
    'id'         : 'mipop1',
    'content'    : 'Menu Items',
    'attributes' : ['aria-haspopup=true'],
    'popup'      : menu_item,
  },
  { 'role'       : 'menuitem',
    'id'         : 'mipop2',
    'content'    : 'Checkbox Items',
    'attributes' : ['aria-haspopup=true'],
    'popup'      : menu_checkbox,
  },
  { 'role'       : 'menuitem',
    'id'         : 'mipop3',
    'content'    : 'Radio Items',
    'attributes' : ['aria-haspopup=true'],
    'popup'      : menu_radio,
  },
  { 'role'       : 'menuitem',
    'id'         : 'mipop4',
    'content'    : 'No Popup',
    'attributes' : ['aria-haspopup=false'],
    'popup'      : [],
  },
]
  
menubar_tests = [
  { 'description'      : "Simple menubar, no popups",    
    'test_id'          : "mi2",
    'role'             : "menuitem",
    'states'           : [],
    'properties'       : [],
    'content'          : "",
    'menu'             : menu_item,
    'attributes'       : [],
    'aria-label'       : "Menubar aria label content",
    'html-label'       : [],
    'aria-labelledby'  : [],    
    'include-node'     : "",    
    'aria-describedby' : "",    
    'title'            : "", 
  },
  { 'description'      : "Menubar with popup menus, focus on a menu item that has a pop up menu",    
    'test_id'          : "mipop1",
    'role'             : "menuitem",
    'states'           : [],
    'properties'       : ['aria-haspopup=true'],
    'content'          : "",
    'menu'             : menu_popup,
    'attributes'       : [],
    'aria-label'       : "Menubar aria label content",
    'html-label'       : [],
    'aria-labelledby'  : [],    
    'include-node'     : "",        
    'aria-describedby' : "",    
    'title'            : "",
  },

]